part of '../repository.dart';

const _kRegularTransactionOfferTypeValue = 'regular';
const _kIntruductoryTransactionOfferTypeValue = 'introductory';
const _kPromotionalTransactionOfferTypeValue = 'promotional';
const _kSubscriptionCodeTransactionOfferTypeValue = 'subscription_code';

class _TransactionOfferTypeDto extends Equatable {
  const _TransactionOfferTypeDto._internal({
    required this.offerTypeString,
  });

  final String offerTypeString;

  @override
  List<Object?> get props {
    return [
      offerTypeString,
    ];
  }

  factory _TransactionOfferTypeDto.fromString(String offerTypeString) {
    return _TransactionOfferTypeDto._internal(
      offerTypeString: offerTypeString,
    );
  }

  factory _TransactionOfferTypeDto.fromEntity(AppStoreTransactionOfferType offerType) {
    late final String offerTypeString;

    if (offerType == AppStoreTransactionOfferType.regular) {
      offerTypeString = _kRegularTransactionOfferTypeValue;
    } else if (offerType == AppStoreTransactionOfferType.introductory) {
      offerTypeString = _kIntruductoryTransactionOfferTypeValue;
    } else if (offerType == AppStoreTransactionOfferType.promotional) {
      offerTypeString = _kPromotionalTransactionOfferTypeValue;
    } else if (offerType == AppStoreTransactionOfferType.subscriptionCode) {
      offerTypeString = _kSubscriptionCodeTransactionOfferTypeValue;
    }

    return _TransactionOfferTypeDto._internal(
      offerTypeString: offerTypeString,
    );
  }

  @override
  String toString() {
    return offerTypeString;
  }

  AppStoreTransactionOfferType toEntity() {
    if (offerTypeString == _kRegularTransactionOfferTypeValue) {
      return AppStoreTransactionOfferType.regular;
    }

    if (offerTypeString == _kIntruductoryTransactionOfferTypeValue) {
      return AppStoreTransactionOfferType.introductory;
    }

    if (offerTypeString == _kPromotionalTransactionOfferTypeValue) {
      return AppStoreTransactionOfferType.promotional;
    }

    if (offerTypeString == _kSubscriptionCodeTransactionOfferTypeValue) {
      return AppStoreTransactionOfferType.subscriptionCode;
    }

    throw UnimplementedError();
  }
}
